GoLang

Explain Anonymous Function in Go Programming | Go Programming Tutorial

Explain Anonymous Function in Go Programming | Go Programming Tutorial

Explain Anonymous Function in Go Programming | Go Programming Tutorial

In this article, you will learn about Anonymous Functions in Go Programming.

What is Anonymous Function in Go Programming Language?

Go Language has a unique feature which is known as an anonymous function. It is a function that does not contain any name. It is very useful when you want to create an inline function. An anonymous function is also called a function literal.

SYNTAX:

func(paramters_list) (return_type) {

  // code..

  // do use return statement if return_type are given

  // if the return_type is not mentioned, then do not use the return statement

return 

} ()

Example:

package main

import “fmt”

func main() {

          func() {

                  fmt.Println(“Welcome to Tutorials Link”)

          }()

}

OUTPUT:

Welcome to Tutorials Link

Important Points:

  • In the Go language, you can assign an anonymous function to a variable. When the user assigns a function to the variable, then the type of the variable becomes a function type and the user can call that variable just like a function call.
  • The user is allowed to pass arguments in the anonymous function.
  • The user can also pass an anonymous function as a function into another function
  • The user can also return an anonymous function from another function.
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.